package com.chang.multithread;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;


import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@Slf4j
public class SleepORYield {
    public static void main(String[] args) {

    Thread t1=new Thread("t1"){
        @Override
        public void run() {
            try {
               TimeUnit.SECONDS.sleep(2);
              } catch (InterruptedException e) {
                e.printStackTrace();
             }
            log.debug("t1线程正在睡眠");
        }
    };

    t1.start();
    log.debug("t1 state: {}"+t1.getState());
    try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
      e.printStackTrace(); }
    log.debug("t1 state: {}",t1.getState());  //TIMED_WAITING阻塞状态
    //sleep让线程进入阻塞状态   yield让线程进入就绪状态
    }


    @Test
    void testYield(){
        Runnable r1=()->{
          int count=0;
          for (;;){
              System.out.println("-----1>"+count++);
          }
        };

        Runnable r2=()->{
          int count=0;
          for (;;){
              //Thread.yield();//让出此刻cpu
              System.out.println("-----2>"+count++);
          }
        };
        Thread t1=new Thread(r1,"线程1");
        Thread t2=new Thread(r2,"线程2");
        t1.start();
        t2.start();
        t1.setPriority(Thread.MIN_PRIORITY);//最小优先级
        t2.setPriority(Thread.MAX_PRIORITY);
    }




}
